import { ResultBean, Product } from '@shared/bean';
import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';

@Component({
  selector: 'app-products-list-edit',
  templateUrl: './edit.component.html',
})
export class ProductsListEditComponent implements OnInit {
  record: any = {};
  i: Product;
  schema: SFSchema = {
    properties: {
      name: { type: 'string', title: '名称' },
      type: { type: 'number', title: '类型' },
      image: { type: 'string', title: '头像'},
      description: {type: 'string', title: '说明'}
    },
    required: ['name'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 60,
      grid: { span: 10 },
    },
    $name: {
      widget: 'input'
    },
    $image: {
      widget: 'upload',
      action: '/upload',
      resReName: 'url'
    },
    $description: {
      widget: 'textarea',
      autosize: {minRows: 2},
      grid: { span: 24 },
    },
  };

  constructor(
    private modal: NzModalRef,
    public msgSrv: NzMessageService,
    public http: _HttpClient,
  ) {}

  ngOnInit(): void {
    if (this.record.id > 0)
    this.http.get(`/api/products/${this.record.id}`).subscribe((res: ResultBean<Product>) => (this.i = res.data));
  }

  save(value: any) {
    this.http.post(`/api/products`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  close() {
    this.modal.destroy();
  }
}
